var _pagination = null;

$(function() {
	initShopSelecter();
});

/**
 * 初始化店面选择器
 */
function initShopSelecter() {
	refreshInput($(".serWrapper"));
	$("#shopIsChoosed").removeAttr("checked");
	$("#shopName").attr("disabled", "disabled");

	$("#shopIsChoosed").bind("click", function() {
		var checked = $(this).attr("checked");
		if (checked) {
			$("#shopName").removeAttr("disabled");
		} else {
			$("#shopName").attr("disabled", "disabled");
			$("#shopName").empty();
		}
	});
}

/**
 * 查询
 */
function search() {

	var shopId = $("#shopName").val();

	_pagination = new Pagination({
		url : '../service/find/list',
		data : {
			shopId : shopId
		},
		success : printListToPage,
		pagerHandler : 'pager'
	});
}

/**
 * 显示
 */
function printListToPage(rst) {
	var response = new qmsk.Response(rst);
	qmsk.dataTable({
		tbodyId : 'serviceList',
		heads : ["shopName", "serviceCode", "serviceName", "expenses", "give", "cost", "createFileTime", "status", "memo", "nums", "serviceId"],
		response : response,
		sup : ['sup', '共 [index] 条'],
		handler : [[10, updateAndDelButton]]
	});
}

function updateAndDelButton(serviceId, row) {
	return "<input class='del' type='button' value='删除' onClick='del(" + serviceId + ")'/>" + "<input type='button' class='common_btn' value='修改' onClick='toUpdate(" + serviceId + ")'/>";
}

/**
 * 打开添加弹出层
 */
function showAddPanel() {
	var shopName = $("#shopName").val();

	$("#saveButton").unbind('click');
	$("#saveButton").bind('click', add);

	if (qmsk.isEmpty(shopName)) {
		artAlert('请选择店面！');
		return;
	}

	$("#shopName > option:selected").clone().appendTo("#addShopName");
	$("#addShopName").attr("disabled", "disabled");

	$("#window_change input[type='text']").val('');
	$("#window_change textarea").val('');

	$("#window_change,#yinying1").show();
	addMargin($("#window_change"));
}

/**
 * 隐藏添加弹出层
 */
function hideAddPanel() {
	$("#window_change,#yinying1").hide();
}

function add() {

	var shopID = $("#addShopName").val();
	var serviceCode = $("#addServiceCode").val();
	var serviceName = $("#addServiceName").val();
	var expenses = $("#addExpenses").val();
	var give = $("#addGiven").val();
	var cost = $("#addCost").val();
	var washCar = $("#addWishCar").val();
	var nums = $("#addNums").val();
	var memo = $("#addMemo").val();

	qmsk.post("../service/insert/" + shopID, {
		serviceCode : serviceCode,
		serviceName : serviceName,
		expenses : expenses,
		give : give,
		cost : cost,
		washCar : washCar,
		nums : nums,
		memo : memo
	}, function() {
		artSuccess("添加成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});

}

function del(serviceId) {
	artConfirm('确定要删除吗？', function() {
		qmsk.post('../service/del/' + serviceId, function() {
			artSuccess("删除成功！");
			_pagination.reload();
		});
	});
}

function toUpdate(serviceId) {
	qmsk.get('../service/detail/' + serviceId, function(response) {

		var shopId = response.getValue("shopID");
		var shopName = response.getValue("ShopName");

		var option = "<option value='" + shopId + "' checked='checked'>" + shopName + "</option>";

		$("#addShopName").empty();
		$("#addShopName").append($(option));
		$("#addShopName").attr('disabled', 'disabled');

		$("#addShopName").val(response.getValue("shopID"));
		$("#addServiceCode").val(response.getValue("serviceCode"));
		$("#addServiceName").val(response.getValue("serviceName"));
		$("#addExpenses").val(response.getValue("expenses"));
		$("#addGiven").val(response.getValue("give"));
		$("#addCost").val(response.getValue("cost"));
		$("#addWishCar").val(response.getValue("washCar"));
		$("#addNums").val(response.getValue("nums"));
		$("#addMemo").val(response.getValue("memo"));

		$("#saveButton").unbind('click');
		$("#saveButton").bind('click', {
			serviceId : serviceId
		}, update);

		$("#window_change,#yinying1").show();
		addMargin($("#window_change"));
	});
}

function update(event) {

	var serviceId = event.data.serviceId;
	var shopID = $("#addShopName").val();
	var serviceCode = $("#addServiceCode").val();
	var serviceName = $("#addServiceName").val();
	var expenses = $("#addExpenses").val();
	var give = $("#addGiven").val();
	var cost = $("#addCost").val();
	var washCar = $("#addWishCar").val();
	var nums = $("#addNums").val();
	var memo = $("#addMemo").val();

	qmsk.post("../service/update/" + shopID + "/" + serviceId, {
		serviceCode : serviceCode,
		serviceName : serviceName,
		expenses : expenses,
		give : give,
		cost : cost,
		washCar : washCar,
		nums : nums,
		memo : memo
	}, function() {
		artSuccess("更新成功！");
		hideAddPanel();
		if (_pagination) {
			_pagination.reload();
		}
	});
}

